﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<%@include file="/common/javascript.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>基本课表</title>
<script type="text/javascript">
    var mainform;
    var viewType = "${viewType}";
    var id = "${id}";
    var courseId=$("#courseId").val();
    var dptId=$("#dptId").val();
    var teaId=$("#teaId").val();
    var gradeId=$("#gradeId").val();
    var classId=$("#classId").val();
    
    $(function(){
        mainform=$("#mainform");
        mainform.ligerForm();
        if($("#classId").val()!='-1'){
        	$("#classId").attr("disabled",false);
        }else{
        	$("#classId").attr("disabled","disabled");
        }
        
        if($("#gradeId").val()!='-1'){
        	$("#gradeId").attr("disabled",false);
        }else{
        	$("#gradeId").attr("disabled","disabled");
        }
        
        if($("#gradeId").val()!='-1' && $("#classId").val()!='-1'){
        	resetOptions($("#dptId").find("option:selected").text(),
					$("#gradeId").find("option:selected").text());
        }
        //渲染按钮
        LG.setFormDefaultBtn(cancel,viewType=="detail" ? null : save);
    });
    
    function save(){
    	var unSelect="";
    	if($("#courseId").val()=='-1'){
    		unSelect+="课程名称、";
    	}
    	if($("#dptId").val()=='-1'){
    		unSelect+="科系名称、";
    	}
    	if($("#teaId").val()=='-1'){
    		unSelect+="教师名称、";
    	}
    	if($("#gradeId").val()=='-1'){
    		unSelect+="年级名称、";
    	}
    	if($("#classId").val()=='-1'){
    		unSelect+="班级名称、";
    	}
    	if(unSelect!=""){
    		LG.showError('请先选择: ' + unSelect);
    	}else{
            LG.submitForm(mainform, function (result) {
            	if(typeof(result)=="string"){
            		result=eval("("+result+")");
            	}
                if (!result.success) {
                    LG.showError('错误:' + result.message);
                }
                else { 
                    LG.showSuccess('保存成功', function () { 
                       LG.callParent("btableList");
                    });
                }
            });
    	}
    }
    
    function cancel(){
        var win = window.parent || window;
        if(win.dialog){
        	win.dialog.close();
        }
    }

    function onGradeChange(){
		if($("#gradeId").val()!='-1'){
			if($("#dptId").val()!='-1'){
				$("#classId").attr("disabled",false);
				resetOptions($("#dptId").find("option:selected").text()
						,$("#gradeId").find("option:selected").text());
	    	}
    	}
    }
    
    function onDeptChange(){
    	if($("#dptId").val()!='-1'){
    		$("#gradeId").attr("disabled",false);
			if($("#gradeId").val()!='-1'){
				$("#classId").attr("disabled",false);
				resetOptions($("#dptId").find("option:selected").text(),
						$("#gradeId").find("option:selected").text());
	    	}
    	}
    }
    
    function resetOptions(t1,t2){
    	document.getElementById('classId').options.length = 0;  //清空原有的option 
    	var str="<option value='-1'>请选择</option>";  
    	jQuery.each(${clas}, function(i,item){
    		if(t1==item.deptTitle && t2==item.gradeTitle){
    			if("${command.claid.clsId}"==item.clsId){
    				str+="<option value='"+item.clsId+"' selected='selected'>"+item.clsTitle+"</option>"
    			}else{
    				str+="<option value='"+item.clsId+"'>"+item.clsTitle+"</option>"
    			}
    		}
    	});     

    	$("#classId").html(str);
    	
    }
    
 </script>
</head>

<body>
	<form name="mainform" method="post" id="mainform"
		action="${ctx }/qso/btable/save">
		<input type="hidden" name="viewType" value="${viewType}" />
		<input type="hidden" name="id" value="${id}" />
		<input type="hidden" name="values" value="" />
		<table cellpadding="0" cellspacing="0" class="l-table-edit"
			style="width: 90%; margin-top: 10px; margin-left: 15px;">
			<tr>
				<td align="right" class="l-table-edit-td" style="width: 20%">课程名称:</td>
				<td align="left" class="l-table-edit-td" style="width: 30%"><select
					name="courseId" id="courseId" ltype="select"
					validate="{required:true}" style="width: 100%">
						<option value="-1">请选择</option>
						<c:forEach items="${courses}" var="course">
							<c:choose>
								<c:when test="${command.couid.id eq course.id}">
									<option value="${course.id}" selected="selected">${course.title}</option>
								</c:when>
								<c:otherwise>
									<option value="${course.id}">${course.title}</option>
								</c:otherwise>
							</c:choose>
						</c:forEach>
				</select></td>
				
				<td align="right" class="l-table-edit-td" style="width: 20%">教师姓名:</td>
				<td align="left" class="l-table-edit-td" style="width: 30%"><select
					name="teaId" id="teaId" ltype="select" validate="{required:true}"
					style="width: 100%">
						<option value="-1">请选择</option>
						<c:forEach items="${teachers}" var="teacher">
							<c:choose>
								<c:when test="${command.teaid.terId eq teacher.terId}">
									<option value="${teacher.terId}" selected="selected">${teacher.terName}</option>
								</c:when>
								<c:otherwise>
									<option value="${teacher.terId}">${teacher.terName}</option>
								</c:otherwise>
							</c:choose>
						</c:forEach>
				</select></td>
			</tr>
			<tr>
				<td align="right" class="l-table-edit-td" style="width: 20%">科系:</td>
				<td align="left" class="l-table-edit-td" style="width: 30%"><select
					name="dptId" id="dptId" ltype="select" validate="{required:true}"
					style="width: 100%" onchange="onDeptChange()">
						<option value="-1">请选择</option>
						<c:forEach items="${depts}" var="dept">
							<c:choose>
								<c:when test="${command.dptid.id eq dept.id}">
									<option value="${dept.id}" selected="selected">${dept.title}</option>
								</c:when>
								<c:otherwise>
									<option value="${dept.id}">${dept.title}</option>
								</c:otherwise>
							</c:choose>
						</c:forEach>
				</select></td>
				
				<td align="right" class="l-table-edit-td" style="width: 20%">年级:</td>
				<td align="left" class="l-table-edit-td" style="width: 30%"><select
					name="gradeId" id="gradeId" ltype="select"
					validate="{required:true}" style="width: 100%" onchange="onGradeChange()">
						<option value="-1">请选择</option>
						<c:forEach items="${grades}" var="grade">
							<c:choose>
								<c:when test="${command.gradeid.gradeId eq grade.gradeId}">
									<option value="${grade.gradeId}" selected="selected">${grade.gradeTitle}</option>
								</c:when>
								<c:otherwise>
									<option value="${grade.gradeId}">${grade.gradeTitle}</option>
								</c:otherwise>
							</c:choose>
						</c:forEach>
				</select></td>
			</tr>
			<tr>
				<td align="right" class="l-table-edit-td" style="width: 20%">班级:</td>
				<td align="left" class="l-table-edit-td" style="width: 30%"><select
					name="classId" id="classId" ltype="select" disabled="disabled"
					validate="{required:true}" style="width: 100%">
						<option value="-1">请选择</option>
						<c:forEach items="${clas}" var="cl">
							<c:choose>
								<c:when test="${command.claid.clsId eq cl.clsId}">
									<option value="${cl.clsId}" selected="selected">${cl.clsTitle}</option>
								</c:when>
								<c:otherwise>
									<option value="${cl.clsId}">${cl.clsTitle}</option>
								</c:otherwise>
							</c:choose>
						</c:forEach>
				</select></td>
			</tr>
		</table>
	</form>
</body>
</html>
